var LevelPlay = {
    _refer: null,
    isPointInSprite: util.isPointInSprite,
    
    sprFlashRegion: null,
    sprPen: null,
    
    currentPenId: 0,
    isFlashingSprite: false,
    disableTouch: false,
    
    TOUCH_BOARD_WIDTH: 512,
    TOUCH_BOARD_HEIGHT: 512,
    GAME_BOARD_X: null,
    GAME_BOARD_Y: null,
    CURRENT_PEN_OFFSET: 80,
    //	mChartboostHandler : ngChartboostLuaHandler:new_local(),	C++函数 
    
    init: function(){
        this.sprPen = this._refer._sprPen;
        this.GAME_BOARD_X = this._refer.GAME_RECT_X_CENTER - this.TOUCH_BOARD_WIDTH / 2;
        this.GAME_BOARD_Y = this._refer.GAME_RECT_Y_CENTER - this.TOUCH_BOARD_HEIGHT / 2;
        
    },
    onEnter: function(refer){
        this._refer = refer;
        this.sprFlashRegion = g_flashRegionSpr;
        this.disableTouch = false;
        this.init();
    },
    onExit: function(){
        this.disableTouch = true;
    },
    onTouch: function(event){
        if (!this.checkTouchPen(event)) {
            this.onTouchBoard(event);
        }
    },
    checkTouchPen: function(event){
        var touchPen = false;
        for (var i = 0; i < 4; i++) {
            if (util.isPointInSprite(this.sprPen[i].instance, event._point.x, event._point.y)) {
                this.selectPen(i);
                touchPen = true;
                return;
            }
        }
        return touchPen;
    },
    selectPen: function(penId){
        cc.log("select pen " + penId);
        if (this._refer._levelState == this._refer._levelPlay && this.currentPenId != penId) {
            var lastPenId = this.currentPenId;
            this.currentPenId = penId;
            var actionBy = cc.MoveBy.create(0.1, cc.p(0, this.CURRENT_PEN_OFFSET));
            this.sprPen[this.currentPenId].instance.runAction(actionBy);
            if (lastPenId >= 0) {
                this.sprPen[lastPenId].instance.runAction(actionBy.reverse());
            }
        }
    },
    onCCBAnimEnd: function(){
    },
    stopFlashing: function(){
        this.stopFlashingSprite(this.sprFlashRegion);
    },
    stopFlashingSprite: function(spr){
        if (this.isFlashingSprite) {
            spr.cleanup();
            spr.setOpacity(255);
            spr.setVisible(false);
            this.isFlashingSprite = false;
        }
    },
    onTouchBoard: function(event){
        var $this = this;
        if (this.disableTouch) {
            return;
        }
        var x = event._point.x, y = event._point.y;
        if ((x >= this.GAME_BOARD_X && x <= this.GAME_BOARD_X + this.TOUCH_BOARD_WIDTH) && (y >= this.GAME_BOARD_Y && y <= this.GAME_BOARD_Y + this.TOUCH_BOARD_HEIGHT)) {
            this._refer.startTimer();
            var px = x - this.GAME_BOARD_X;
            var py = y - this.GAME_BOARD_Y;
            var i = (this.TOUCH_BOARD_HEIGHT - py) / 2; //scale to 256
            var j = px / 2;
            sound.playFillSound();
            fillColor(i, j, this.currentPenId, false);
            
            var isLevelPassed = checkPassLevel();
            if (isLevelPassed) {
                cc.log("level passed");
                this._refer.pauseLevel();
                this._refer.calcScore();
                
                this.stopFlashing();
                this.disableTouch = true;
                this.sprFlashRegion.setVisible(true);
                this.sprFlashRegion.runAction(cc.Sequence.create(cc.FadeIn.create(0.2), cc.FadeOut.create(0.3), cc.CallFunc.create($this.onPassed, this)));
                this.isFlashingSprite = true;
            } else {
                this.stopFlashing();
                var isFailed = checkFailRegion();
                if (isFailed) {
                    cc.log("level failed")
                    this._refer.addFailCount();
                    this.sprFlashRegion.runAction(cc.Sequence.create(cc.Blink.create(2, 4), cc.CallFunc.create($this.onFailFlashEnd, this)));
                    this.isFlashingSprite = true;
                }
            }
        }
    },
    getChartboostLocation: function(){
    },
    ChartBoost_DidDismissInterstitial: function(){
    },
    onPassed: function(node){
        this.stopFlashingSprite(node);
        this._refer.setState(this._refer._levelWin);
        sound.playSuccessSound();
    },
    onFailFlashEnd: function(node){
        this.stopFlashingSprite(node);
    }
};
